# !/usr/bin/env
# -*- encoding:utf-8 -*-

__author__ = 'shawhen'

import socket

def socket_recv(sock, bytes_2_recv):
    """
    无异常
    在接受到bytes_2_recv个数据之前,不会返回,除非出错
    :param sock: socket
    :param bytes_2_recv: 要接受的数据长度
    :return: bytes,有可能为空
    """
    data = b''
    if bytes_2_recv <= 0:
        return data
    try:
        remain_bytes_to_recv = bytes_2_recv
        while remain_bytes_to_recv > 0:
            data_i = sock.recv(remain_bytes_to_recv)
            if len(data_i) == 0:
                break
            remain_bytes_to_recv -= len(data_i)
            data += data_i
    except Exception as e:
        import traceback
        traceback.print_exc()
    return data